<script lang="ts" setup>
import { defineProps, ref, watchEffect } from 'vue'
// 传入 true 显示组件 传入 false 隐藏组件
let props = defineProps<{ show: boolean }>()
const maskRef = ref()
watchEffect(() => {
  if (props.show) {
    setTimeout(() => {
      maskRef.value.style.display = 'block'
      setTimeout(() => {
        maskRef.value.style.backgroundColor = 'rgba(0,0,0,.3)'
      }, 0);
    }, 0);
  } else {
    setTimeout(() => {
      maskRef.value.style.backgroundColor = 'transparent'
      setTimeout(() => {
        maskRef.value.style.display = 'none'
      }, 300);
    }, 0);
  }
})
</script>
<template>
  <div class="mask" ref="maskRef"></div>
  <Transition name="fade">
    <div class="box" v-if="show">
      <slot></slot>
    </div>
  </Transition>
</template>
<style scoped lang="scss">
.mask {
  position: fixed;
  top: 0;
  left: 0;
  width: 100vw;
  height: 100vh;
  z-index: 100;
  transition: all .3s;
  background-color: transparent;
}

.box {
  position: absolute;
  top: 0;
  left: 0;
  width: 100vw;
  height: 100vh;
  background-color: #fff;
  z-index: 101;
}

.fade-enter-from,
.fade-leave-to {
  transform: translateX(100vw);
}

.fade-enter-active,
.fade-leave-active {
  transition: all 0.5s;
}

.fade-enter-to,
.fade-leave-from {
  transform: translateX(0);
}
</style>
